{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This lab explores the dog licensing data set: [https://data.cityofnewyork.us/Health/NYC-Dog-Licensing-Dataset/nu7n-tubp/data](https://data.cityofnewyork.us/Health/NYC-Dog-Licensing-Dataset/nu7n-tubp/data)\n", "\n", "Filter this data set to only contain entries with the license expiring after today (Aug. 29) before downloading it as a CSV file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "import seaborn as sns\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read the CSV file into a dataframe called `dogs`, and display the dataframe to make sure it was loaded correctly." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two of the columns are dates, so make them into `datetime` objects." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get an over-view of the dataset by calling the `describe()` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Are the summary statistics (mean, standard deviation, min, etc.) meaningful for all of these columns?\n", "2. Are we missing any data?\n", "\n", "Answers:\n", "1. No. The only column they might be meaningful for is `Extract Year`.\n", "2. Yes, notice we have no borough information, despite it being a column. Also, some of the columns are missing!\n", "\n", "To display columns with non-numeric data, we need to use the following command." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dogs.describe(include = [\"O\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What's the most common dog name?\n", "\n", "To visualize the distribution of a qualitative or categorica column, we will make a bar chart (as in MAT 128). Let's do this for the `AnimalGender` column.\n", "\n", "First get the value counts." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gender_counts = dogs['AnimalGender'].value_counts()\n", "gender_counts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Are there roughly 50% male and female dogs?\n", "\n", "Next we plot the counts:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gender_counts.plot.bar()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add a title and axis labels. Remember to repeat your original plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's plot the distribution of another categorial column, `BreedName`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is this plot as readable as the gender one?\n", "\n", "We can fix this by just plotting the top 10 breeds using filtering. First, look at the counts to see what the cut-off should be." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It looks like 1000 would be a good cut-off. We can do the filter one of two ways:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "top_filter = breed_counts >=1000\n", "breed_counts[top_filter].plot.bar()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "breed_counts[breed_counts >= 1000].plot.bar()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What's the most popular breed of dog?\n", "\n", "In Lab 1, we only filtered by a single criteria, but we can filter by multiple criteria using `&` (and) and `|` (or). For example, if we wanted to count the number of dog licenses issues in August to a Chihuahua:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "aug_chihuahua_filter = (dogs[\"BreedName\"] == \"Chihuahua\") & (dogs[\"LicenseIssuedDate\"].dt.month == 8)\n", "len(dogs[aug_chihuahua_filter])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or if we wanted to count the number of dogs named BELLA or dogs whose license expires in 2020:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bella_2020_filter = (dogs[\"AnimalName\"] == \"BELLA\") | (dogs[\"LicenseExpiredDate\"].dt.year == 2020)\n", "len(dogs[bella_2020_filter])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With a partner or by yourself, answer the following questions:\n", "\n", "1. How many dogs are registered in the Lehman zip code (10468)? " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Plot a bar chart of the number of dog licenses in the top 10 zip codes? Which zip code has the most dog licenses and where is it located (type a zip code into Google maps to see the area)?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. How many female Labrador Retreivers are licensed?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. How many licenses are due to expired by the end of September?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. Ask and answer your own question(s) about this dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Challenge question: Can you find the oldest dog in the dataset?" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }